/***************************************************************************
 *   Copyright © 2010 by Nikolay Kornaev                                   *
 *   n.kornaev@gmail.com                                                   *
 *                                                                         *
 *                    This file is part of neuma.                          *
 *                                                                         *
 *   neuma is free software; you can redistribute it and/or modify it      *
 *   under the terms of the GNU General Public License as published by     *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   neuma is distributed in the hope that it will be useful,              *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, see <http://www.gnu.org/licenses/>   *
 ***************************************************************************/

#include <QtCore>
#include <QtGui>

#include "SettingsHandler.h"

namespace Neuma
{
  SettingsHandler::SettingsHandler()
  {
#ifdef STILL_TESTING
    QSettings set(QSettings::IniFormat, QSettings::UserScope, "slavoslovie.ru", "neuma");
#else // STILL_TESTING
    QSettings set;
#endif // STILL_TESTING
    /// создаём список настроек с путями и значениями-по-умолчанию
    init();
    /// читаем все настройки
    foreach(Setting *s, _data)
        s->setValue(_set.value(s->getPath(), s->getDefValue()));
  }

  void SettingsHandler::writeSettings()
  {
    foreach(Setting *s, _data)
        if (!s->getValue().isNull())
          _set.setValue(s->getPath(), s->getValue());
  }

  void SettingsHandler::init()
  {
    insert(PageMarginBottom, "Page/MarginBottom", 20.001);
    insert(PageMarginLeft, "Page/MarginLeft", 20.001);
    insert(PageMarginRight, "Page/MarginRight", 20.001);
    insert(PageMarginTop, "Page/MarginTop", 20.001);
    insert(PageOrientation, "Page/Orientation", QPrinter::Portrait);
    insert(PageSize, "Page/Size", QPrinter::A4);
    insert(RendererExportResolution, "Renderer/ExportResolution", 300);
    insert(RendererFontColorNeuma, "Renderer/FontColorNeuma", "#000000");
    insert(RendererFontColorMarks, "Renderer/FontColorMarks", "#ff0000");
    insert(RendererFontColorSyllabe, "Renderer/FontColorSyllabe", "#000000");
    insert(RendererFontNameNeuma, "Renderer/FontNameNeuma", "Paraklht");
    insert(RendererFontNameSyllabe, "Renderer/FontNameSyllabe", "Irmologion UCS");
    insert(RendererFontSizeNeuma, "Renderer/FontSizeNeuma", 18);
    insert(RendererFontSizeSyllabe, "Renderer/FontSizeSyllabe", 16);
    insert(RendererFontsListNeuma, "Renderer/FontsListNeuma", "Paraklht");
    insert(RendererFontsListSyllabe, "Renderer/FontsListSyllabe", "Irmologion UCS");
    insert(RendererSpaceBlocksHorizontal, "Renderer/SpaceBlocksHorizontal", 2.5);
    insert(RendererSpaceBlocksVertical, "Renderer/SpaceBlocksVertical", 1.0);
    insert(RendererSpaceLines, "Renderer/SpaceLines", 5.0);
    insert(RendererOutlinePage, "Renderer/Outline/Page", false);
    insert(RendererOutlineNeuma, "Renderer/Outline/Neuma", false);
    insert(RendererOutlineSyllabe, "Renderer/Outline/Syllabe", false);
    insert(RendererOutlineColorPage, "Renderer/Outline/ColorPage", "#888888");
    insert(RendererOutlineColorNeuma, "Renderer/Outline/ColorNeuma", "#888888");
    insert(RendererOutlineColorSyllabe, "Renderer/Outline/ColorSyllabe", "#888888");
    insert(RendererNeumaOverSyllabe, "Renderer/NeumaOverSyllabe", true);
    insert(RendererNeumaTruncateMarks, "Renderer/NeumaTruncateMarks", true);
  }

  QVariant SettingsHandler::val(const SettingName name) const
  {
    return _data.value(name)->getValue();
  }

  void SettingsHandler::setVal(const SettingName name, const QVariant val)
  {
    _data[name]->setValue(val);
  }

  void SettingsHandler::insert(const SettingName name, const QString path, const QVariant defVal)
  {
    _data.insert(name, new Setting(path, defVal));
  }
}
